import 'package:flutter/material.dart';

/// create 2019-07-29 by cai

class TextButton extends StatelessWidget {
  final String text;
  final Function onPressed;
  final Function onLongPressed;
  final double width;
  final double height;
  final double fontSize;
  final Color textColor;
  final Color backgroundColor;
  final TextStyle textStyle;
  final double radius;
  final AlignmentGeometry align;
  final BoxBorder border;
  final Color disableColor;
  final bool enable;

  const TextButton({
    Key key,
    @required this.text,
    @required this.onPressed,
    this.onLongPressed,
    this.width = 88,
    this.height = 33,
    this.fontSize,
    this.backgroundColor,
    this.textStyle = const TextStyle(color: Colors.white, fontSize: 14.0),
    this.radius = 5,
    this.textColor,
    this.align = Alignment.center,
    this.border,
    this.enable = true,
    this.disableColor,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final backgroundColor =
        this.backgroundColor ?? Theme.of(context).primaryColor;

    final disableColor =
        this.disableColor ?? Theme.of(context).disabledColor ?? Colors.grey;

    Widget w;

    w = Text(
      this.text,
      style: _style,
    );

    w = InkWell(
      onTap: onPressed,
      onLongPress: onLongPressed,
      child: Align(
        alignment: align,
        child: w,
      ),
    );

    w = Material(
      color: enable != true ? disableColor : backgroundColor,
      borderRadius: BorderRadius.circular(radius),
      child: SizedBox(
        width: this.width,
        height: this.height,
        child: w,
      ),
    );

    if (border != null) {
      w = Container(
        child: w,
        decoration: BoxDecoration(
          border: border,
          borderRadius: BorderRadius.circular(radius),
        ),
      );
    } else {
      w = Container(
        child: w,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(radius),
        ),
      );
    }

    if (radius > 0) {
      w = ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: w,
      );
    }

    if (enable != true) {
      w = IgnorePointer(
        child: w,
      );
    }

    return w;
  }

  TextStyle get _style {
    var style = this.textStyle;
    if (this.fontSize != null) {
      style = style.copyWith(fontSize: this.fontSize);
    }
    if (this.textColor != null) {
      style = style.copyWith(color: this.textColor);
    }
    return style;
  }
}
